package
{
	/**
	 * @author robin for Tudou AlbumPlayer 2010-03-26
	 * 本作品采用知识共享组织署名 3.0 Unported许可协议进行授权。
	 * 查看本许可协议的复制件，请访问http://creativecommons.org/licenses/by/3.0/
	 * 或写信到美国加利福尼亚州三藩市霍华德街543号5层知识共享组织（邮编 94105）索取。
	 * @email:rman@live.cn
	 * @blog:www.xncat.com
	 */
	
	import com.monboo.album.AlbumFacade;
	import com.monboo.album.command.AlbumEvent;
	import com.monboo.album.common.AlbumAction;
	import com.monboo.album.common.AlbumConst;
	import com.monboo.utils.ContextMenuPlus;
	import com.monboo.utils.GetStage;
	import com.monboo.album.view.*;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	public class AlbumLite extends Sprite
	{			
		public var hideText:Boolean = false;
		
		public var sourceWidth:Number;
		public var sourceHeight:Number;
		
		public var albumWidth:Number;
		public var albumHeight:Number;
		public var imageWidth:Number;
		public var imageHeight:Number;
		
		public var imageX:Number;
		public var imageY:Number;
		public var albumTextX:Number;
		public var albumTextY:Number;
		
		public var autoTime:Number;
		public var autoPlay:Boolean = true;
		public var gotoStop:Boolean = true;
		public var scaleIn:Boolean = true;
		
		private var _stage:Stage;
		private var facade:AlbumFacade;
		private var contextMenuUrl:Object = new Object();
		private var albumData:Object;
		
		private var imageAreaMediator:ImageAreaMediator;
		private var imageInfo:AlbumInfoMediator;
		private var fullScreenMediator:FullScreenMediator;
		private var albumMediator:AlbumMediator;
		
		private var transitionObj:Object;
		
		public function AlbumLite(_albumData:Object)
		{
			albumData = _albumData;
			
			facade = AlbumFacade.getInstance();
			
			addEventListener(Event.ADDED_TO_STAGE,addToStageFunc);
		
			initProperty();	
		}
		private function addToStageFunc(e:Event):void{
			_stage = this.parent.stage;
			GetStage.initGetStage(_stage);
			
			_stage.align = StageAlign.TOP_LEFT;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			
			initContextMenu();
			
			facade.startup(this);
			getModule();
			startAutoPlay();
			
		}
		
		
		/**
		 * setTransitionMask方法可以在转场文件加载成功后，把转场对象设置为albumPlayer的转场文件
		 * @param tansArr 每张图片对应的转场编号，这是一个数组
		 * @param tranSwf 是一个Object，转场swf文件中需要包含返回每一种转场效果对象的公共方法
		 * @example 下面例子是外调转场文件，并把加载到的转场设置与albumPlayer的转场。
		 * <listing>
		 *	var __tranSwf:Class;
		 *	var tranSwf:Object;
		 *	var transUrl:String = "./transition/albumTransition.swf";
		 *	loadTransition();
		 *	
		 *	function loadTransition():void {
		 *		var loadTrans:Loader = new Loader();
		 *		var transContext:LoaderContext = new LoaderContext();
		 *		transContext.checkPolicyFile=true;
		 *		transContext.applicationDomain=new ApplicationDomain(ApplicationDomain.currentDomain);
		 *		loadTrans.load(new URLRequest(transUrl),transContext);
		 *		loadTrans.contentLoaderInfo.addEventListener(Event.COMPLETE,transitionLoadedFun);
		 *	}
		 *	function transitionLoadedFun(e:Event):void {
		 *		__tranSwf=e.target.applicationDomain.getDefinition("AlbumTransition") as Class;
		 *		tranSwf = new __tranSwf();
		 *		
		 *		albumPlayer.setTransition(albumData["transArr"],tranSwf);
		 *		trace("[Transition loaded successfully!]");
		 *	}
		 * </listing>
		 * @return void
		 **/
		public function setTransition(tansArr:Array,tranSwf:Object):void{
			transitionObj = new Object();
			transitionObj["arr"] = tansArr;
			transitionObj["tranSwf"] = tranSwf;
		}
		
		
		/**
		 * 把只在全屏时显示的内容，面板或功能条放在showMap数组中，使用showMap作为参数执行fullScreenShow，
		 * 那么这些显示对象就会在全屏的时候显示出来，退出全屏状态时则消失。
		 * @param showMap
		 * @return void
		 **/
		public function fullScreenShow(showMap:Array):void{
			fullScreenMediator.registShow(showMap);
		}
		
		/**
		 * 把全屏与退出全屏时都显示，但全屏时停靠在屏幕的"top","right","bottom","left"四种位置上，
		 * 把相关设置做成对象，把每一个对象放在parkMap中，使用parkMap作为参数执行fullScreenPark。
		 * 每个对象的书写格式：parkMap = [{target:cPanel_top,align:"top",offset:0,pad:cPanel_top.height},
		 *					{target:cPanel_bottom,align:"bottom",offset:0,pad:cPanel_bottom.height}]
		 * @param parkMap
		 * @return void
		 **/
		public function fullScreenPark(parkMap:Array):void{
			fullScreenMediator.registPark(parkMap);
		}
		
		/**
		 * 把只在进入全屏时消失的内容，面板或功能条放在hideMap数组中，使用hideMap作为参数执行fullScreenHide，
		 * 那么这些显示对象就会在进入全屏状态时消失，退出全屏的时候显示出来。
		 * @param hideMap
		 * @return void
		 **/
		public function fullScreenHide(hideMap:Array):void{
			fullScreenMediator.registHide(hideMap);
		}
		
		/**
		 * 可以获取相册的自动播放状态，true为自动播放中，false停止自动播放；
		 * 配合这个自读属性可以在侦听到syncAutoState事件后，同步albumPlayer对象外部的自动播放按钮的ui状态
		 * @return Boolean
		 **/
		public function get autoState():Boolean{
			return imageInfo.autoState;
		}
		
		/**
		 * 可获得相册的标题内容
		 * @return String
		 **/
		public function get title():String{
			return imageInfo.title;
		}
		
		/**
		 * 可获得相册的总图片数
		 * @return int
		 **/
		public function get total():int{
			return imageInfo.total;
		}
		
		/**
		 * 可获得相册的当前图片是第几张
		 * @return int
		 **/
		public function get current():int{
			return imageInfo.current;
		}
		
		
		/**
		 * 可获得相册的当前图片对应的文字内容
		 * @return String
		 **/
		public function get text():String{
			return imageInfo.text;
		}
		
		/**
		 * 相册转到下一张图片
		 * @return void
		 **/
		public function next():void{
			if(!AlbumAction.pageState("next",imageInfo.current,imageInfo.total)){
				if(imageInfo.gotoStop){
					imageInfo.autoState = false;
					facade.sendNotification(AlbumEvent.SYN_AUTO_STATE);
				}				
				imageInfo.current = imageInfo.current +1;
				
				facade.sendNotification(AlbumEvent.NEXT_IMAGE);
				facade.sendNotification(AlbumEvent.RESET_IMAGE);
			}
		}
		
		/**
		 * 相册转到上一张图片
		 * @return void
		 **/
		public function pre():void{
			if(!AlbumAction.pageState("pre",imageInfo.current)){
				if(imageInfo.gotoStop){
					imageInfo.autoState = false;
					facade.sendNotification(AlbumEvent.SYN_AUTO_STATE);
				}				
				imageInfo.current = imageInfo.current -1;
				
				facade.sendNotification(AlbumEvent.PRE_IMAGE);
				facade.sendNotification(AlbumEvent.RESET_IMAGE);
			}
		}
		
		/**
		 * 播放相册
		 * @return void
		 **/
		public function play():void{		
			imageInfo.autoState = true;	
			facade.sendNotification(AlbumEvent.AUTO_PLAY);			
		}
		
		/**
		 * 停止播放相册
		 * @return void
		 **/
		public function stop():void{
			imageInfo.autoState = false;	
			facade.sendNotification(AlbumEvent.KILL_DELAY_CALL);
			facade.sendNotification(AlbumEvent.SYN_AUTO_STATE);
		}
		
		/**
		 * 相册转到第一张图片
		 * @return void
		 **/
		public function first():void{
			if(imageInfo.gotoStop){
				imageInfo.autoState = false;
				facade.sendNotification(AlbumEvent.SYN_AUTO_STATE);
			}
			facade.sendNotification(AlbumEvent.FIRST_IMAGE);
			
			imageInfo.current = 0;		
			
			facade.sendNotification(AlbumEvent.RESET_IMAGE);
		}
		
		/**
		 * 相册转到最后一张图片
		 * @return void
		 **/
		public function last():void{
			if(imageInfo.gotoStop){
				imageInfo.autoState = false;
				facade.sendNotification(AlbumEvent.SYN_AUTO_STATE);
			}
			facade.sendNotification(AlbumEvent.LAST_IMAGE);
			
			imageInfo.current = imageInfo.total;
			
			facade.sendNotification(AlbumEvent.RESET_IMAGE);
		}
		
		
		/**
		 * 相册转到参数指定的图片数
		 * @return void
		 **/
		public function gotoImage(n:int):void{
			if(imageInfo.current != n){
				if(imageInfo.gotoStop){
					imageInfo.autoState = false;
					facade.sendNotification(AlbumEvent.SYN_AUTO_STATE);
				}
				imageInfo.current = n;
				facade.sendNotification(AlbumEvent.THUMB_SHOWIMAGE);
				
				facade.sendNotification(AlbumEvent.RESET_IMAGE);
			}
		}
		
		
		/**
		 * 相册全屏方法
		 * @return void
		 **/
		public function fullScreen():void{
			facade.sendNotification(AlbumEvent.FULL_SCREEN);
		}
		
		/**
		 * 相册打开与关闭相框与动画效果的方法
		 * @return void
		 **/
		public function openEffect(state:Boolean):void{
			if(state){
				facade.sendNotification(AlbumEvent.OPEN_EFFECT);
			}else{
				facade.sendNotification(AlbumEvent.CLOSE_EFFECT);
			}
		}
		
		/**
		 * @private
		 * @internal getAlbumData方法用于内部其它类调用，获取从外部传入的数据对象
		 * @return Object
		 *  
		 **/
		public function getAlbumData():Object{
			return albumData;
		}
		
		/**
		 * @private
		 * @internal getTransitionMask方法用于内部其它类调用，获取从外部设置的转场对象
		 * @return Object
		 *  
		 **/
		public function getTransitionMask():Object{
			return transitionObj;
		}
		
		/**
		 * @private
		 * @相册启动时，如果自动播放为true，将执行启动自动播放相册
		 * @return void
		 *  
		 **/
		private function startAutoPlay():void{
			if(autoPlay){
				if(imageInfo.total>1){
					imageInfo.autoState = true;
				}
			}
			facade.sendNotification(AlbumEvent.START_AUTO_PLAY);
		}
		
		/**
		 * @private
		 * @这个方法用于获取AlbumConst中的默认相册属性值，并初始相册的默认属性
		 * @return void
		 *  
		 **/
		private function initProperty():void{
			autoTime = AlbumConst.autoTime;
			
			sourceWidth = AlbumConst.sourceWidth;
			sourceHeight = AlbumConst.sourceHeight;
			
			albumWidth = AlbumConst.albumWidth;
			albumHeight = AlbumConst.albumHeight;
			
			imageWidth = AlbumConst.imageWidth;
			imageHeight = AlbumConst.imageHeight;
			
			imageY = AlbumConst.imageY;
			imageX = AlbumConst.imageX;
			
			albumTextX = AlbumConst.albumTextX;
			albumTextY = AlbumConst.albumTextY;
			
		}
		
		/**
		 * @private
		 * @获取需要被公共调用的显示模块
		 * imageInfo可以让外部对象取得相册的内部公共属性
		 * fullScreenMediator可以让外部对象取得相册的全屏公共属性与方法
		 * @return void
		 *  
		 **/
		private function getModule():void{
			imageInfo = facade.retrieveMediator( AlbumInfoMediator.NAME ) as AlbumInfoMediator;
			fullScreenMediator = facade.retrieveMediator( FullScreenMediator.NAME ) as FullScreenMediator;
		}
		
		/**
		 * @private
		 * @初始右键菜单，添加右键菜单中的项目
		 * @return void
		 *  
		 **/
		private function initContextMenu():void{			
			var cm:ContextMenuPlus = new ContextMenuPlus(true);
			contextMenu = cm.contextMenu;
			cm.addCustom(contextMenuClick, AlbumConst.menuTextArr[0]);
			
			contextMenuUrl[AlbumConst.menuTextArr[0]] = AlbumConst.menuUrlArr[0];
		}
		private function contextMenuClick(e:ContextMenuEvent):void{
			navigateToURL(new URLRequest(contextMenuUrl[e.currentTarget.caption]),"_blank");
		}
	}
}